<template>
  <div class="box">
    <div class="headImg" :class="{ others: !isSelf }">{{ name.substring(0, 1) }}</div>
    <div class="con" :class="{ self: isSelf }">
      <p class="name">{{ name }} </p>
      <p class="message">{{ mess }}</p>
      <p class="time">{{showTime}}</p>
    </div>
  </div>
</template>

<script>
import dayjs from "dayjs"
export default {
  props: {
    name: {
      type: String,
      default: "You"
    },
    isSelf: {
      type: Boolean,
      default: true
    },
    mess: {
      type: String,
      required: true
    },
    time: {
      type: Number
    }
  },
  computed: {
    showTime(){
      return dayjs(this.time).format("YYYY-MM-DD HH:mm:ss")
    }
  },
  inject: ["chatMessage"]
}
</script>

<style lang="scss" scoped>
.box {
  margin: 5px;
  padding: 10px;
  box-sizing: border-box;
  font-size: 12px;
  display: flex;

  .headImg {
    width: 30px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    background-color: #5436DA;
    border-radius: 50%;
    color: white;
  }

  .others {
    background-color: #41B883;
  }

  .self {
    order: -1;

    .name,.time {
      text-align: right;
    }
  }

  .con {
    flex: 1;
    margin: 5px 20px;

    .name {
      color: #999;
      margin-bottom: 10px;
    }

    .time {
      font-size: 12px;
      color: rgb(255, 57, 57);
      margin-top: 5px;
      text-indent: 10px;
    }

    .message {
      line-height: 20px;
      border: 1px solid #ccc;
      box-sizing: border-box;
      padding: 10px;
      border-radius: 20px;
      // 数字自动换行
      word-break: break-all;
    }
  }
}
</style>